<xsl:stylesheet version='1.0'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au">

  <db:article>
    <db:info>
      <db:title>Extract Manufacturer File</db:title>

      <db:copyright>
        <db:year>2012</db:year>
        <db:year>2009</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").  The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>1.11</db:revnumber>
          <db:date>2012-11-07</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Added documentation.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.1</db:revnumber>
          <db:date>2012-11-02</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Sorted output based on manufacturer code (ascending).</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0.1</db:revnumber>
          <db:date>2012-05-31</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Improve formatting of structured addresses.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2009-09-21</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet produces the Manufacturer extract file (<db:filename>Mnfr yyyymmdd.txt</db:filename>) from a PBS XML document.</db:para>
    <db:para>The Manufacturer extract provides name, address, telephone and facsimile number information for the sponsors of drugs listed in the schedule.</db:para>
    <db:para>The source document must be a PBS XML document.</db:para>
    <db:para>The result document is a text file.</db:para>
    <db:para>The default field delimeter in this file is the exclamation mark (!).</db:para>
    <db:table>
      <db:tgroup cols="4" align="left" colsep="1" rowsep="1">
        <db:colspec colnum="3" align="right"/>
        <db:thead>
          <db:row>
            <db:entry>Field Name</db:entry>
            <db:entry>Format</db:entry>
            <db:entry>Maximum Length</db:entry>
            <db:entry>Description</db:entry>
          </db:row>
        </db:thead>
        <db:tbody>
          <db:row>
            <db:entry>Manufacturer Code</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>2</db:entry>
            <db:entry>Two character alphabetic code in the Schedule Book</db:entry>
          </db:row>
          <db:row>
            <db:entry>Manufacturer Name</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>120</db:entry>
            <db:entry></db:entry>
          </db:row>
          <db:row>
            <db:entry>Street Address</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>180</db:entry>
            <db:entry></db:entry>
          </db:row>
          <db:row>
            <db:entry>Telephone Number</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>16</db:entry>
            <db:entry>Numeric characters</db:entry>
          </db:row>
          <db:row>
            <db:entry>Facsimile Number</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>16</db:entry>
            <db:entry>Numeric characters</db:entry>
          </db:row>
        </db:tbody>
      </db:tgroup>
    </db:table>
  </db:article>

  <xsl:output method='text'/>
  <xsl:strip-space elements='*'/>
  <xsl:preserve-space elements='db:para'/>

  <xsl:variable name='delimiter' select='"!"'/>

  <db:template name='root'>
    <db:refpurpose>Check Document Version</db:refpurpose>
    <db:para>Make sure that the source document uses the correct version of the PBS XML Schema.</db:para>
  </db:template>
  <xsl:template match='/'>
    <xsl:variable name='major' select='substring-before(/*/@version, ".")'/>

    <xsl:choose>
      <xsl:when test='$major >= 2'>
        <xsl:apply-templates select='//pbs:organisations-list/pbs:organisation'>
          <xsl:sort select='pbs:code'/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate='yes'>This stylesheet is designed to operate upon a Schedule XML document that conforms to version 2.0 (or later) of the PBS XML Schema.  See http://xml.pbs.gov.au/ for more information on the PBS XML Schema, and for a version of this XSL stylesheet that operates on documents that conform to an earlier version of the schema.</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match='pbs:organisation'>
    <xsl:apply-templates select='pbs:code'/>
    <xsl:value-of select='$delimiter'/>
    <!-- name (max 120 chars ) -->
    <xsl:call-template name='truncate'>
      <xsl:with-param name='text'>
        <xsl:apply-templates select='db:title'/>
      </xsl:with-param>
      <xsl:with-param name='length' select='120'/>
    </xsl:call-template>
    <xsl:value-of select='$delimiter'/>
    <!-- street address (max 180 chars) -->
    <xsl:call-template name='truncate'>
      <xsl:with-param name='text'>
        <xsl:apply-templates select='pbs:address'/>
      </xsl:with-param>
      <xsl:with-param name='length' select='180'/>
    </xsl:call-template>
    <xsl:value-of select='$delimiter'/>
    <!-- telephone number (max 16 chars) -->
    <xsl:call-template name='truncate'>
      <xsl:with-param name='text'>
        <xsl:apply-templates select='db:phone'/>
      </xsl:with-param>
      <xsl:with-param name='length' select='16'/>
    </xsl:call-template>
    <xsl:value-of select='$delimiter'/>
    <!-- facsimile number (max 16 chars) -->
    <xsl:call-template name='truncate'>
      <xsl:with-param name='text'>
        <xsl:apply-templates select='db:fax'/>
      </xsl:with-param>
      <xsl:with-param name='length' select='16'/>
    </xsl:call-template>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

  <xsl:template match='text()'>
    <xsl:if test='parent::db:street/preceding-sibling::* or
                  (ancestor::pbs:address and ../preceding-sibling::*)'>
      <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:value-of select='normalize-space()'/>
  </xsl:template>

  <!-- Make sure strings aren't too long, and at the same time
       translate special characters into an equivalent
       7-bit ASCII character
    -->
  <xsl:variable name='to'>-''"" </xsl:variable>
  <xsl:template name='truncate'>
    <xsl:param name='text'/>
    <xsl:param name='length' select='0'/>

    <xsl:value-of
      select='translate(substring($text, 1, $length),
              "&#x2014;&#x2018;&#x2019;&#x201c;&#x201d;&#xa0;",
              $to)'/>
  </xsl:template>
</xsl:stylesheet>
